#include "mainwin.h"
#include "ui_mainwin.h"

bool IsLogin = false;
USERINFO UsrInfo;

MainWin::MainWin(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWin)
{
    ui->setupUi(this);

    Init();
}

MainWin::~MainWin()
{
    delete ui;
    delete pNote;
    delete books;
    delete wRecord;
    delete login;
    delete rgst;
}

// 函数
void MainWin::Init()
{
    // 初始化数据库
//    InitMySql();
    // 初始化控件
    InitCtrls();

}

void MainWin::InitCtrls()
{
    ui->wgt_menu->setGeometry(0, 0, ui->wgt_menu->width(), this->height());
    ui->line_mid->setGeometry(ui->wgt_menu->width(), 0, ui->line_mid->width(), this->height());
    ui->wgt_right->setGeometry(ui->wgt_menu->width() + ui->line_mid->width(), 0,
        this->width() - ui->wgt_menu->width() - ui->line_mid->width(), this->height());

    ui->wgt_menu->setLayout(ui->vLayout_menu);

    // 初始化操作界面，密码本，书籍，工作记录
    pNote = new PasswdNote;
    pNote->setParent(ui->wgt_right);
    pNote->show();
    books = new Books;
    books->setParent(ui->wgt_right);
    books->hide();
    wRecord = new WorkRecord;
    wRecord->setParent(ui->wgt_right);
    wRecord->hide();
    login = new Login;
    login->setParent(ui->wgt_right);
    login->hide();
    rgst = new Register;
    rgst->setParent(ui->wgt_right);
    rgst->hide();
}

void MainWin::OpenOneWindow(int WindowFlag)
{
    switch (WindowFlag) {
    case PASSWDNOTE:
    {
        pNote->show();
        pNote->LoadPasswdNote();
        books->hide();
        wRecord->hide();
        login->hide();
        rgst->hide();
    }
        break;
    case BOOKS:
    {
        books->show();
        pNote->hide();
        wRecord->hide();
        login->hide();
        rgst->hide();
    }
        break;
    case WORKRECORD:
    {
        wRecord->show();
        pNote->hide();
        books->hide();
        login->hide();
        rgst->hide();
    }
        break;
    case LOGIN:
    {
        login->show();
        wRecord->hide();
        pNote->hide();
        books->hide();
        rgst->hide();
    }
        break;
    case REGISTER:
    {
        rgst->show();
        wRecord->hide();
        pNote->hide();
        books->hide();
        login->hide();
    }
        break;
    default:
        break;
    }
}

// -------------------事件-----------------------
void MainWin::resizeEvent(QResizeEvent *e)
{
    ui->wgt_menu->setGeometry(0, 0, ui->wgt_menu->width(), this->height());
    ui->line_mid->setGeometry(ui->wgt_menu->width(), 0, ui->line_mid->width(), this->height());
    ui->wgt_right->setGeometry(ui->wgt_menu->width() + ui->line_mid->width(), 0,
        this->width() - ui->wgt_menu->width() - ui->line_mid->width(), this->height());
}

void MainWin::on_pBtn_PasswdNote_clicked()
{
    OpenOneWindow(PASSWDNOTE);
}

void MainWin::on_pBtn_Books_clicked()
{
    OpenOneWindow(BOOKS);
}

void MainWin::on_pBtn_WorkRecord_clicked()
{
    OpenOneWindow(WORKRECORD);
}

void MainWin::on_pBtn_login_clicked()
{
    OpenOneWindow(LOGIN);
}

void MainWin::on_pBtn_register_clicked()
{
    OpenOneWindow(REGISTER);
}
